﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.BLL;

using OA.Model;
using OA.Web.Areas.Member.Controllers;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;


namespace OA.Web.Areas.Management.Controllers
{
    [Area("Management")]
    public class KnowledgeCategoryController : BaseMemberController
    {

        private IMemoryCache _cache;
        public KnowledgeCategoryController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        [MyActionFilter("206001", "知识类别", "管理中心",true, "iconzhishi")]
        public IActionResult Index()
        {
            return View();
        }

        [MyActionFilter("206001", "知识类别")]
        public IActionResult GetData(string keyword, int page = 1, int limit = 20)
        {
            Expression<Func<KnowledgeGroup, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = m => (m.Name.Contains(keyword));
            }
            PageList<SimpleKnowledgeGroup> list = this.GetPageList<KnowledgeGroup, SimpleKnowledgeGroup>(where, page, limit, SimpleKnowledgeGroup.Map);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.RowCount,
                data = list.List
            };
            return Json(result);
        }

        [HttpGet]
        [MyActionFilter("206001001", "添加类别", "知识类别", false, "")]
        public IActionResult Create()
        {
            return View(new KnowledgeGroup());
        }




        [HttpPost]
        [MyActionFilter("206001001", "添加类别", "知识类别", false, "")]
        public IActionResult Create(KnowledgeGroup model)
        {
            BaseResult baseResult = new BaseResult();

            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);                 
                return Json(baseResult);
            }
            else
            {
                this.AddInfo<KnowledgeGroup>(model);
                baseResult.Msg = "知识类别添加成功";
                return Json(baseResult);

            }

        }

        [HttpGet]
        [MyActionFilter("206001002", "编辑类别", "知识类别", false, "")]
        public IActionResult Edit(int id)
        {
            KnowledgeGroup infoKnowledgeGroup = this.GetInfo<KnowledgeGroup>(id);
            if (infoKnowledgeGroup == null)
            {
                return Redirect("/Admin/Home/ShowInfo?msg=" + System.Web.HttpUtility.UrlEncode("沒有找到该知识分组", Encoding.UTF8));
            }
            return View(infoKnowledgeGroup);
        }

        [HttpPost]
        [MyActionFilter("206001002", "编辑类别", "知识类别", false, "")]
        public IActionResult Edit(int id, KnowledgeGroup model)
        {
            BaseResult baseResult = new BaseResult();

            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);
                return Json(baseResult);
            }
            KnowledgeGroup infoPageGroup = this.GetInfo<KnowledgeGroup>(id);
            if (infoPageGroup == null)
            {
                baseResult.State = 400;
                baseResult.Msg = "沒有找到该知识分组";
                return Json(baseResult);
                 
            }
            this.Update<KnowledgeGroup>(infoPageGroup, model, new string[] { "Name", "Sort" });
            baseResult.Msg = "知识类别修改成功";
            return Json(baseResult);
        }

        [HttpPost]
        [MyActionFilter("206001003", "删除类别", "知识类别", false, "")]
        public IActionResult Delete(long id)
        {
            Expression<Func<KnowledgeGroup, bool>> where = m => m.ID == id;
            this.Delete<KnowledgeGroup>(where);
            return Json(new { State = 200, Message = "" });
        }
    }
}
